﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using System.Web.Security;
using Cake_Management.Models;

namespace Cake_Management.Controllers
{
    public class HT_OrderController : Controller
    {
        // GET: HT_Order
        public ActionResult Index()
        {
            return View();
        }

        // GET: HT_Order/Details/5

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Order order)
        {
            order.CreateDate = DateTime.Now;
            order.Date = DateTime.Now;
            new BLL.B_Order().Add(order);
            return Json(new { Code = 200, Message = "添加成功" });
        }

        public ActionResult GetAll()
        {
            var result =new BLL.B_Order().GetAll();
            var nb = from a in result
                     select new
                     {
                         a.Id,
                         a.OrderNum,
                         a.status,
                         CreateDate = a.CreateDate.ToString(),
                         a.Total,
                         a.Freight,
                         a.CustomerId,
                         a.AddressId,
                         a.Leave,
                         a.Invoice,
                         a.Notes,
                         Date = a.Date.ToString(),
                         a.Hour,
                         Operation =$"<div data-id='{a.Id}' class='btn btn-warning data-edit' onclick='Edit(this)'>编辑</div>      <div data-id='{a.Id}' class='btn btn-danger data-del'onclick='Del(this)'>删除</div>"
                     };
            var data = new { data = nb };
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        // GET: HT_Order/Edit/5
        public ActionResult Edit(int id)
        {
            var data =new BLL.B_Order().FindById(id);
            return PartialView(data);
        }


        // POST: HT_Order/Edit/5
        [HttpPost]
        public ActionResult Edit(Order order)
        {
            order.CreateDate = DateTime.Now;
            order.Date = DateTime.Now;
            //new BLL.B_Order().Updata(order.Id, order);

            //return Json(new { Code = 200, Message = "修改成功" });

            try
            {
                new BLL.B_Order().Updata(order.Id, order);
                return Json(new { Code = 200, Message = "更改成功" });
            }
            catch (Exception ex)
            {
                return Json(new { Code = 500, Message = ex.Message });
            }
            
        }


        // GET: HT_Order/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                new BLL.B_Order().Delete(id);
                return Json(new { Code = 200, Message = "删除成功" });
            }
            catch(Exception ex)
            {
                return Json(new { Code = 500, Message = ex.Message });
            }
        }  

        
    }
}
